package com.vlong.town.controller.admin.comm;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.comm.CommInfo;
import com.vlong.town.service.comm.CommInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 信息互联信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/comm")
public class CommInfoController extends BaseController {

    // Fields

    @Autowired
    private CommInfoService commInfoService;// 信息互联信息业务实现类

    /**
     * 进入信息互联信息列表页面
     * @return comm/list_comm
     */
    @RequestMapping("/list")
    public String gotoCommInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<CommInfo> commInfoList = this.commInfoService.queryList(title, content, desc);
        PageInfo<CommInfo> pageInfo = new PageInfo<CommInfo>(commInfoList);
        PageTag<CommInfo> pt = new PageTag<CommInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        for(CommInfo commInfo : commInfoList) {
            commInfo.setContent(DataUtil.delHTMLTag(commInfo.getContent(), 0));
        }
        request.setAttribute("commInfoList", commInfoList);
        return "admin/comm/list_comm";
    }

    /**
     * 进入信息互联信息添加页面
     * @return comm/add_comm
     */
    @GetMapping("/addComm")
    public String gotoCommInofAddPage() {
        return "admin/comm/add_comm";
    }

    /**
     * 进入信息互联信息修改页面
     * @return comm/edit_comm
     */
    @GetMapping("/editComm/{infoId}")
    public String gotoCommInofEditPage(@PathVariable(required=true, value="infoId") Long infoId) {
        CommInfo commInfo = this.commInfoService.queryById(infoId);
        request.setAttribute("commInfo", commInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + commInfo.getThumbnail());
        return "admin/comm/edit_comm";
    }

    /**
     * 添加信息互联信息
     * @param title 信息互联标题
     * @param content 信息互联内容
     * @param desc 信息互联简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddComm")
    public DataMessage addComm(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("信息互联标题不合法");
            return dm;
        }
        this.commInfoService.add(1L, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("信息互联添加成功");
        return dm;
    }

    /**
     * 修改信息互联信息
     * @param title 信息互联标题
     * @param content 信息互联内容
     * @param desc 信息互联简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditComm")
    public DataMessage editComm(
            @RequestParam(name="infoId", required=true) Long infoId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("信息互联标题不合法");
            return dm;
        }
        this.commInfoService.update(infoId, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("信息互联修改成功");
        return dm;
    }

    /**
     * 删除信息互联
     * @param infoId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="infoId") Long infoId) {
        DataMessage dm = new DataMessage();
        if(infoId != null && infoId > 0L) {
            this.commInfoService.delete(infoId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("信息互联删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除信息互联
     * @param infoIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="infoIds") String infoIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(infoIds, 1, 255)) {
            String[] infoIdArray = infoIds.split(",");
            for(String infoId : infoIdArray) {
                this.commInfoService.delete(Long.parseLong(infoId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("信息互联删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

}
